import java.util.List;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Admin extends User {
    public Admin(String id, String username, String password, String phone) {
        super(id, username, password, phone, "admin");
    }
    
    public void addUser(UserService userService, Scanner scanner) {
        List<User> users = userService.getUsers();
        System.out.println("请输入用户类型（admin/manager/clerk）：");
        String role = scanner.nextLine();
        if (!role.equals("admin") && !role.equals("manager") && !role.equals("clerk")) {
            System.out.println("用户类型无效。");
            return;
        }
        System.out.println("请输入用户名（唯一）：");
        String username = scanner.nextLine();
        for (User u : users) {
            if (u.getUsername().equals(username)) {
                System.out.println("用户名已存在。");
                return;
            }
        }
        System.out.println("请输入手机号（唯一）：");
        String phone = scanner.nextLine();
        for (User u : users) {
            if (u.getPhone().equals(phone)) {
                System.out.println("手机号已存在。");
                return;
            }
        }
        String id = java.util.UUID.randomUUID().toString();
        String password = "123456";
        User newUser;
        switch (role) {
            case "admin": newUser = new Admin(id, username, password, phone); break;
            case "manager": newUser = new Manager(id, username, password, phone); break;
            case "clerk": newUser = new Clerk(id, username, password, phone); break;
            default: System.out.println("用户类型无效。"); return;
        }
        userService.addUser(newUser);
        System.out.println("用户添加成功，初始密码为123456。");
        
        // 记录日志
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        LogEntry.addLog(new LogEntry(time, "用户管理", this.getUsername(), "添加用户: " + username + " (" + role + ")"));
    }
    
    public void deleteUser(UserService userService, Scanner scanner) {
        List<User> users = userService.getUsers();
        System.out.println("请输入要删除的用户名或ID：");
        String input = scanner.nextLine();
        User toDelete = null;
        for (User u : users) {
            if (u.getUsername().equals(input) || u.getId().equals(input)) {
                toDelete = u;
                break;
            }
        }
        if (toDelete == null) {
            System.out.println("未找到该用户。");
            return;
        }
        if (toDelete instanceof Admin && toDelete.getUsername().equals("admin")) {
            System.out.println("不能删除admin管理员账号。");
            return;
        }
        System.out.printf("确定要删除用户 %s (ID: %s)? (y/n): ", toDelete.getUsername(), toDelete.getId());
        String confirm = scanner.nextLine();
        if (confirm.equalsIgnoreCase("y")) {
            userService.deleteUser(toDelete.getId());
            System.out.println("用户已删除。");
            
            // 记录日志
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            LogEntry.addLog(new LogEntry(time, "用户管理", this.getUsername(), "删除用户: " + toDelete.getUsername()));
        } else {
            System.out.println("操作已取消。");
        }
    }
    
    public void modifyUser(UserService userService, Scanner scanner) {
        List<User> users = userService.getUsers();
        System.out.println("请输入要修改的用户名或ID：");
        String input = scanner.nextLine();
        User toModify = null;
        for (User u : users) {
            if (u.getUsername().equals(input) || u.getId().equals(input)) {
                toModify = u;
                break;
            }
        }
        if (toModify == null) {
            System.out.println("未找到该用户。");
            return;
        }
        if (toModify instanceof Admin) {
            System.out.println("不允许修改管理员账号。");
            return;
        }
        System.out.printf("当前类型: %s, 手机: %s\n", toModify.getRole(), toModify.getPhone());
        System.out.println("请输入新用户类型（manager/clerk，回车跳过）：");
        String newRole = scanner.nextLine();
        if (!newRole.isEmpty() && !newRole.equals("manager") && !newRole.equals("clerk")) {
            System.out.println("用户类型无效。");
            return;
        }
        System.out.println("请输入新手机号（回车跳过）：");
        String newPhone = scanner.nextLine();
        if (!newPhone.isEmpty()) {
            for (User u : users) {
                if (!u.getId().equals(toModify.getId()) && u.getPhone().equals(newPhone)) {
                    System.out.println("手机号已存在。");
                    return;
                }
            }
            toModify.setPhone(newPhone);
        }
        if (!newRole.isEmpty() && !newRole.equals(toModify.getRole())) {
            User newUser = null;
            if (newRole.equals("manager")) {
                newUser = new Manager(toModify.getId(), toModify.getUsername(), toModify.getPassword(), toModify.getPhone());
            } else if (newRole.equals("clerk")) {
                newUser = new Clerk(toModify.getId(), toModify.getUsername(), toModify.getPassword(), toModify.getPhone());
            }
            if (newUser != null) {
                userService.updateUser(newUser);
                System.out.println("用户类型已修改。");
                
                // 记录日志
                String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                LogEntry.addLog(new LogEntry(time, "用户管理", this.getUsername(), "修改用户: " + toModify.getUsername() + " -> " + newRole));
            }
        } else {
            if (!newPhone.isEmpty()) {
                userService.updateUser(toModify);
                System.out.println("手机号已修改。");
                
                // 记录日志
                String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                LogEntry.addLog(new LogEntry(time, "用户管理", this.getUsername(), "修改用户手机号: " + toModify.getUsername()));
            } else {
                System.out.println("手机号已修改（如有修改）。");
            }
        }
    }
    
    public void listAllUsers(UserService userService) {
        List<User> users = userService.getUsers();
        System.out.println("\n所有用户信息:");
        for (User u : users) {
            System.out.printf("ID: %s, 用户名: %s, 类型: %s, 手机: %s\n", u.getId(), u.getUsername(), u.getRole(), u.getPhone());
        }
    }
    
    public void manageUsers(UserService userService, Scanner scanner) {
        while (true) {
            System.out.println("\n--- 用户管理 ---");
            System.out.println("1. 列出所有用户");
            System.out.println("2. 增加用户");
            System.out.println("3. 删除用户");
            System.out.println("4. 修改用户");
            System.out.println("0. 返回上一级");
            System.out.print("请选择: ");
            String choice = scanner.nextLine();
            switch (choice) {
                case "1": listAllUsers(userService); break;
                case "2": addUser(userService, scanner); break;
                case "3": deleteUser(userService, scanner); break;
                case "4": modifyUser(userService, scanner); break;
                case "0": return;
                default: System.out.println("无效选择，请重试。");
            }
        }
    }
    
    public void viewLogs() {
        LogEntry.showLogs();
    }
} 